#include "VStartPage.h"
#include "ui_VStartPage.h"
#include "VFunctions.h"
#include "VGlobal.h"

#include <QIcon>
#include <QListWidgetItem>
#include <QFile>
#include <QFileInfo>
#include <QVariant>
#include <QDebug>
#include <QRegularExpression>
#include <QVariant>

VStartPage::VStartPage(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::VStartPage)
{
    ui->setupUi(this);

    ui->listwidget_info->setSelectionMode(QAbstractItemView:: NoSelection);
    ui->listwidget_old->setSelectionMode(QAbstractItemView:: NoSelection);
    ui->listwidget_start->setSelectionMode(QAbstractItemView:: NoSelection);

    // font
    this->setFont(parent->font());
    qDebug() << parent->font().family();

    QFont font(this->font());
    font.setPointSize(21);

    ui->label->setFont(font);
    ui->label_2->setFont(font);
    ui->label_3->setFont(font);
#if 0
    m_manager = new QNetworkAccessManager(this);//新建QNetworkAccessManager对象
    connect(m_manager, &QNetworkAccessManager::finished, this, &VStartPage::slot_replayFinished); //关联信号和槽
//    m_manager->get(QNetworkRequest(QUrl("https://bbs.xiuno.com/")));//发送请求
     m_manager->get(QNetworkRequest(QUrl("https://www.51yishen.com/")));//发送请求
#endif
    //load_start();

    // connent

}

VStartPage::~VStartPage()
{
    delete ui;
}


void VStartPage::load_historical()
{
    ui->listwidget_old->clear();

    QListWidgetItem * item;
    QString text = VFunctions::read_file_text(VGlobal::historical_record_path);
    QStringList list = text.split("\r\n");
    foreach(QString path, list)
    {
        if(path.length() == 0)
        {
            continue;
        }

        if(QFile(path).exists() == false)
        {
            continue;
        }

        item = new QListWidgetItem(ui->listwidget_old);
        item->setText(QString("项目:%1 \r\n路径:%2").arg(QFileInfo(path).fileName()).arg(path));
        item->setIcon(QIcon(VGlobal::icon_path.value("project")));
        item->setData(0, QVariant::fromValue(path));
    }
}

void VStartPage::load_start()
{
    QListWidgetItem * item;
    item = new QListWidgetItem(ui->listwidget_start);
    item->setText("新建项目");
    item->setIcon(QIcon(VGlobal::icon_path.value("new_project")));

    item = new QListWidgetItem(ui->listwidget_start);
    item->setText("打开项目");
    item->setIcon(QIcon(VGlobal::icon_path.value("file_open")));
}

void VStartPage::load_information()
{

}


void VStartPage::on_listwidget_start_itemClicked(QListWidgetItem *item)
{
    emit signal_startpage_action(SignalType::START, item->text());
}

void VStartPage::on_listwidget_old_itemClicked(QListWidgetItem *item)
{
    QString path = item->data(0).value<QString>();
    emit signal_startpage_action(SignalType::HISTORICAL, path);
}

void VStartPage::slot_replayFinished(QNetworkReply *reply)
{
    QTextCodec *codec = QTextCodec::codecForName("utf8");
        //使用utf8编码, 这样可以显示中文
    QString str = codec->toUnicode(reply->readAll());
    reply->deleteLater();//最后要释放replay对象

//    qDebug() << str;

    QRegularExpression reg;
    reg.setPattern("<a href=\"(thread-\\d+.htm)\">(.+?)</a>");
    reg.setPatternOptions(QRegularExpression::DotMatchesEverythingOption);
    QRegularExpressionMatch match;
    match = reg.match(str);
    while(match.hasMatch())
    {
        auto item = new QListWidgetItem(ui->listwidget_info);
        item->setText(match.captured(2));
        item->setIcon(QIcon(VGlobal::icon_path.value("message")));
        item->setData(Qt::UserRole, QVariant::fromValue(match.captured(1)));
        match = reg.match(str, match.capturedStart() + match.capturedLength());
    }

}

void VStartPage::on_listwidget_info_itemClicked(QListWidgetItem *item)
{
    QString data = item->data(Qt::UserRole).value<QString>();
    QDesktopServices::openUrl(QString("https://www.51yishen.com/%1").arg(data));
}
